﻿using System.ComponentModel;
using System.Linq;
using System.Windows.Data;
using DataModel;
using P_PersonalDetails.DataModels;
using P_PersonalDetails.Service.SlalomRanking;


namespace P_PersonalDetails.Service.SlalomRanking
{
    public partial class SlalomRankingsData
    {
        public ICollectionView SingleDivisions
        {
            get
            {
                CollectionViewSource view = new CollectionViewSource();
                view.Source = this.Rankings.Select((S) => new SlalomRankingDM(S));
                view.Filter += (s, e) =>
                {
                    e.Accepted = !((DataModels.SlalomRankingDM)e.Item).HasPartner;
                };

                return view.View;
            }
        }
        public ICollectionView DoubleDivisions
        {
            get
            {
                CollectionViewSource view = new CollectionViewSource();
                view.Source = this.Rankings.Select((S)=> new SlalomRankingDM(S));

                view.Filter += (s, e) =>
                {
                    e.Accepted = ((DataModels.SlalomRankingDM)e.Item).HasPartner;
                };
                return view.View;
            }
        }

        public void AddDoubleDivision()
        {
            var SRV = ServiceConfig.NewSlalomRankingClient();
            SRV.AddSlalomRankingCompleted += new System.EventHandler<AddSlalomRankingCompletedEventArgs>(Value_AddSlalomRankingCompleted);
            SRV.AddSlalomRankingAsync(this.ID);
        }

        void Value_AddSlalomRankingCompleted(object sender, AddSlalomRankingCompletedEventArgs e)
        {
            this.Rankings.Add(new SlalomRankingData() { ID = e.Result, HasPartner=true });
            this.RaisePropertyChanged("DoubleDivisions");
        }

        public void DeleteDoubleDivision(int DivisionID)
        {
            var SRV = ServiceConfig.NewSlalomRankingClient();
            SRV.DeleteSlalomRankingDataAsync(DivisionID);
            this.Rankings.Remove(this.Rankings.First((D)=>D.ID==DivisionID));
            this.RaisePropertyChanged("DoubleDivisions");
        }
    }

}
namespace P_PersonalDetails.DataModels
{
    public class SlalomRankingDM:DataModelBase
    {
        SlalomRankingData D;
        public SlalomRankingDM(SlalomRankingData D)
        {
            this.D = D;
        }
        public int ID { get { return D.ID; } }
        public bool HasPartner { get { return D.HasPartner; } }

        public string ClassName { get { return D.ClassName; } }

        public int? ClassRC
        {
            get { return D.ClassRC; }
            set
            {
                if (D.ClassRC != value)
                {
                    D.ClassRC = value;
                    OnPropertyChanged("ClassRC");
                }
            }
        }

        public int? DivisionRCL2
        {
            get { return D.DivisionRCL2; }
            set
            {
                if (D.DivisionRCL2 != value)
                {
                    D.DivisionRCL2 = value;
                    OnPropertyChanged("DivisionRCL2");
                }
            }
        }

        public int? BibNumber
        {
            get { return D.BibNumber; }
            set
            {
                if (D.BibNumber != value)
                {
                    D.BibNumber = value;
                    OnPropertyChanged("BibNumber");
                }
            }
        }
        public int? PartnerID
        {
            get { return D.PartnerID; }
            set
            {
                if (D.PartnerID != value)
                {
                    D.PartnerID = value;
                    OnPropertyChanged("PartnerID");
                }
            }
        }
        public string PartnerName
        {
            get { return D.PartnerName; }
            set
            {
                if (D.PartnerName != value)
                {
                    D.PartnerName = value;
                    OnPropertyChanged("PartnerName");
                }
            }
        }


        protected override void ComitNewObject()
        {
            throw new System.NotImplementedException();
        }

        protected override void PropertyChangedMethod(string PropertyName, object PropertyValue)
        {
            base.PropertyChangedMethod(PropertyName, PropertyValue);
            base.SetComitAction(PropertyName, PropertyValue, ComitUpdateProp);
        }
        private void ComitUpdateProp(DataModelBase ths, string PropertyName, object value)
        {
            SRV.UpdateSlalomRankingAsync(D.ID, PropertyName, value);
        }


        SlalomRankingServiceClient SRV = ServiceConfig.NewSlalomRankingClient();
    }
}


